package com.money.tracker.auth.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.money.tracker.auth.entity.req.LoginReq;
import com.money.tracker.auth.entity.rsp.*;
import com.money.tracker.auth.service.ISysMenuService;
import com.money.tracker.auth.service.SysLoginService;
import com.money.tracker.common.entity.rsp.IResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 登录控制器
 * <p>
 *
 * @author zhangshuai
 * &#064;@date 2024/09/25
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class SysLoginController {

    private final SysLoginService loginService;
    private final ISysMenuService sysMenuService;

    /**
     * 发送电子邮件
     *
     * @param loginReq
     * @return {@link IResponse }
     */
    @SaIgnore
    @Validated
    @PostMapping("/email/send")
    public IResponse<?> sendEmail(@RequestBody LoginReq loginReq) {
        loginService.sendEmail(loginReq);
        return IResponse.success();
    }

    /**
     * 注册
     *
     * @param req
     * @return {@link IResponse }
     */
    @SaIgnore
    @PostMapping("register")
    public IResponse<LoginRsp> register(@RequestBody LoginReq req) {
        return IResponse.success(loginService.register(req));
    }

    /**
     * 获取用户详情
     * @return
     */
    @PostMapping("/getUserDetail")
    public IResponse<UserInfoRsp> getUserDetail() {
        return IResponse.success(loginService.getUserDetail());
    }

    /**
     * 登录
     *
     * @param loginReq
     * @return {@link IResponse }<{@link LoginRsp }>
     */
    @SaIgnore
    @PostMapping("/login")
    public IResponse<LoginRsp> login(@Validated @RequestBody LoginReq loginReq) {
        return IResponse.success(loginService.login(loginReq));
    }

    /**
     * 生成验证码
     *
     * @return
     */
    @SaIgnore
    @Validated
    @PostMapping("/captchaImage")
    public IResponse<CaptChaRsp> getCode() {
        return IResponse.success(loginService.getCode());
    }



    /**
     * 退出
     * @return {@link IResponse }<{@link ? }>
     */

    @SaIgnore
    @PostMapping("/logout")
    public IResponse<?> logout() {
        loginService.logout();
        return IResponse.success("退出成功");
    }



    /**
     * 获取路由配置信息
     *
     * @return {@link IResponse }<{@link List }<{@link RouterRsp }>>
     */

    @PostMapping("/getRouters")
    public IResponse<List<RouterRsp>> getRouters() {
        return IResponse.success(sysMenuService.getRouters());
    }
}
